﻿using System;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using YArchitech.Revit;

namespace YJKDragCurveJig
{
	public class JigEdgeInfo
	{
		public XYZ StartPoint
		{
			get
			{
				return this.m_StartPoint;
			}
			set
			{
				this.m_StartPoint = value;
			}
		}

		public XYZ EndPoint
		{
			get
			{
				return this.m_EndPoint;
			}
			set
			{
				this.m_EndPoint = value;
			}
		}

		public double Bulge
		{
			get
			{
				return this.m_Bulge;
			}
			set
			{
				this.m_Bulge = value;
			}
		}

		public bool IsArc
		{
			get
			{
				return !JigGeometry.IsEqual(this.m_Bulge, 0.0);
			}
		}

		public bool IsNull()
		{
			return this.m_StartPoint == null || this.m_EndPoint == null;
		}

		public JigEdgeInfo(JigEdgeInfo rhs)
		{
			this.m_StartPoint = rhs.m_StartPoint;
			this.m_EndPoint = rhs.m_EndPoint;
			this.m_Bulge = rhs.m_Bulge;
		}

		public JigEdgeInfo(XYZ startPoint, XYZ endPoint, double bulge)
		{
			this.m_StartPoint = startPoint;
			this.m_EndPoint = endPoint;
			this.m_Bulge = bulge;
		}

		public JigEdgeInfo(Curve curve)
		{
			if (curve.GetType() == typeof(Line))
			{
				this.m_StartPoint = curve.GetEndPoint(0);
				this.m_EndPoint = curve.GetEndPoint(1);
				this.m_Bulge = 0.0;
				return;
			}
			if (curve.GetType() == typeof(Arc))
			{
				Arc arc = curve as Arc;
				this.m_StartPoint = curve.GetEndPoint(0);
				this.m_EndPoint = curve.GetEndPoint(1);
				if (!arc.Normal.IsAlmostEqualTo(XYZ.BasisZ))
				{
					this.m_StartPoint = curve.GetEndPoint(1);
					this.m_EndPoint = curve.GetEndPoint(0);
				}
				this.m_Bulge = JigGeometry.GetBulge(this.m_StartPoint, this.m_EndPoint, arc.Center, true);
			}
		}

		public JigEdgeInfo TransformTo(Transform matrix)
		{
			XYZ startPoint = JigGeometry.TransformPoint(this.StartPoint, matrix);
			XYZ endPoint = JigGeometry.TransformPoint(this.EndPoint, matrix);
			return new JigEdgeInfo(startPoint, endPoint, this.m_Bulge);
		}

		public void TransformSelf(Transform matrix)
		{
			this.m_StartPoint = JigGeometry.TransformPoint(this.StartPoint, matrix);
			this.m_EndPoint = JigGeometry.TransformPoint(this.EndPoint, matrix);
		}

		public Curve ConvertTo(Application app)
		{
			Curve result = null;
			if (this.IsNull())
			{
				return result;
			}
			XYZ xyz = this.Get2DXYZ(this.StartPoint);
			XYZ xyz2 = this.Get2DXYZ(this.EndPoint);
			if (xyz.IsAlmostEqualTo(xyz2))
			{
				return result;
			}
			if (this.IsArc)
			{
				XYZ xyz3 = null;
				double num = 0.0;
				double dSAngle = 0.0;
				double dEAngle = 0.0;
				JigGeometry.GetArcInfoWithBulge(xyz, xyz2, this.Bulge, ref xyz3, ref num, ref dSAngle, ref dEAngle);
				double angle = JigGeometry.BetweenTheAngles(dSAngle, dEAngle, true) / 2.0;
				XYZ pt = xyz - xyz3;
				XYZ radianPnt = xyz3 + JigGeometry.RotateTo(pt, angle, XYZ.BasisZ);
				return app.CreatYJKArc(xyz, xyz2, radianPnt);
			}
			return Line.CreateBound(xyz, xyz2);
		}

		private XYZ Get2DXYZ(XYZ point3d)
		{
			return new XYZ(point3d.X, point3d.Y, 0.0);
		}

		private XYZ m_StartPoint;

		private XYZ m_EndPoint;

		private double m_Bulge;
	}
}
